/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#include <bitset>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll maxn = 11000;
bitset<10002> d[maxn];
ll n, m;
struct node
{
    ll y, next_s;
} e[maxn * 5];
bool vis[maxn];
bool vis_e[maxn * 5];
ll head[maxn], cnt;
ll out[maxn];

void init()
{
    memset(head, -1, sizeof(head));
    cnt = 0;
    memset(vis, false, sizeof(vis));
    memset(vis_e, false, sizeof(vis_e));
}

void add(ll x, ll y)
{
    e[cnt].y = y;
    e[cnt].next_s = head[x];
    head[x] = cnt++;
    return;
}
void dfs(ll id)
{
    vis[id] = true;
    for (ll i = head[id]; i != -1; i = e[i].next_s)
    {

        // if (vis_e[i])
        //     continue;
        ll y = e[i].y;
        // cout << id << " " << y << endl;
        bitset<10002> temp = (d[y] | d[id]);
        if (temp.count() == d[y].count())
        {
            // cout << id << " " << y << endl;
            continue;
        }
        else
        {
            d[y] = (d[y] | d[id]);
            dfs(y);
        }
    }
    return;
}
int main()
{
    IOS;
    ll x, y;
    cin >> n >> m;
    init();
    for (ll i = 1; i <= m; i++)
    {
        cin >> x >> y;
        add(x, y);
        out[x]++;
    }
    ll cnt_0 = 0;
    for (ll i = 1; i <= n; i++)
    {
        if (out[i] == 0)
        {
            cnt_0++;
        }
        d[i].reset();
        d[i].set(i, 1);
        // cout << d[i].count() << endl;
    }
    if (cnt_0 > 1)
    {
        cout << 0 << endl;
        return 0;
    }
    for (ll i = 1; i <= n; i++)
    {
        // if(vis[i])
        //     continue;
        dfs(i);
        // for (ll j=1;j<=n;j++){
        //     // cout << d[j].count()<<endl;
        // }
        // dfs(i);
        // for (ll j=1;j<=n;j++){
        //     // cout << d[j].count()<<endl;
        // }
    }
    ll ans = 0;
    for (ll i = 1; i <= n; i++)
    {
        // cout << d[i].count() << endl;
        if (d[i].count() == n)
        {
            // cout << i << endl;
            ans++;
        }
    }
    cout << ans << endl;
    getchar();
    getchar();
    return 0;
}
// 5 6
// 1 2
// 2 3
// 3 1
// 1 4
// 4 5
// 5 4